package com.zmn.plat.dubbo.impl.orderconfig.returnvisitcontent;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.manager.common.database.annotation.ReadOnlyConnection;
import com.zmn.plat.common.dictionary.PlatErrorCodeDict;
import com.zmn.plat.common.dto.orderconfig.returnvisitcontent.ReturnVisitCategoryDIO;
import com.zmn.plat.common.dto.orderconfig.returnvisitcontent.ReturnVisitCategoryDRO;
import com.zmn.plat.dubbo.interfaces.orderconfig.returnvisitcontent.ReturnVisitCategListRemoteService;
import com.zmn.plat.dubbo.utils.DubboConsts;
import com.zmn.plat.model.entity.orderconfig.returnvisitcontent.ReturnVisitCategory;
import com.zmn.plat.model.entity.orderconfig.returnvisitcontent.ReturnVisitCategoryQuery;
import com.zmn.plat.services.interfaces.orderconfig.returnvisitcontent.ReturnVisitCategoryService;

import lombok.extern.slf4j.Slf4j;

/**
 * 类描述:回访分类dubbo接口实现类
 *
 * @auther: tanghongjun
 * @since: 2020年4月15日11:27:03
 */
@Service(version = DubboConsts.INTERFACE_VERSION, validation = "true")
@Slf4j
public class ReturnVisitCategListRemoteServiceImpl implements ReturnVisitCategListRemoteService {

    @Resource
    ReturnVisitCategoryService returnVisitCategoryService;

    @Override
    @ReadOnlyConnection
    public ResponseDTO<ReturnVisitCategoryDRO> getById(Integer categId) {
        log.info("getById 参数为：categId={}", categId);
        if (Objects.isNull(categId)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        ReturnVisitCategory returnVisitCategory = returnVisitCategoryService.findById(categId);

        if (Objects.isNull(returnVisitCategory)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }

        ReturnVisitCategoryDRO returnVisitCategoryDRO = new ReturnVisitCategoryDRO();
        BeanUtils.copyProperties(returnVisitCategory, returnVisitCategoryDRO);
        return ResponseDTO.success(returnVisitCategoryDRO);
    }

    @Override
    @ReadOnlyConnection
    public ResponseDTO<List<ReturnVisitCategoryDRO>> listByQuery(ReturnVisitCategoryDIO returnVisitCategDIO) {
        log.info("listByQuery 参数为：returnVisitCategDIO={}", returnVisitCategDIO);
        if (Objects.isNull(returnVisitCategDIO)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_PANAM_NULL);
        }

        ReturnVisitCategoryQuery returnVisitCategoryQuery = new ReturnVisitCategoryQuery();
        BeanUtils.copyProperties(returnVisitCategDIO, returnVisitCategoryQuery);
        List<ReturnVisitCategory> returnVisitCategoryList = returnVisitCategoryService.listByQuery(returnVisitCategoryQuery);

        if (CollectionUtil.isNullOrEmpty(returnVisitCategoryList)) {
            return ResponseDTO.fromErrorCodeTable(PlatErrorCodeDict.ERROR_CODE_RESULT_NULL);
        }

        List<ReturnVisitCategoryDRO> dros = returnVisitCategoryList.stream().map(returnVisitCategory -> {
            ReturnVisitCategoryDRO returnVisitCategoryDRO = new ReturnVisitCategoryDRO();
            BeanUtils.copyProperties(returnVisitCategory, returnVisitCategoryDRO);
            return returnVisitCategoryDRO;
        }).collect(Collectors.toList());

        return ResponseDTO.success(dros);
    }
}
